const express = require("express");
//引入路由器
const userRouter = require("./routers/user.js");
const categoryRouter = require("./routers/category.js");
const productRouter = require("./routers/product.js");
const product_imageRouter = require("./routers/product_image.js");
const cartRouter = require("./routers/cart.js");
const collectionRouter = require("./routers/collection.js");
const homeRouter=require('./routers/home');
const orderRouter = require("./routers/order.js");
const addressRouter=require('./routers/address');

const multerUpload = require('./routers/upload');

const session = require("express-session");
const cors = require('cors');

const app = express();

let corsOptions = {
  origin:[/http/,"http://localhost:8080"], //数组元素可以是字符串，正则表达式
  credentials:true,
  optionsSuccessStatus: 200 // some legacy browsers (IE11, various SmartTVs) choke on 204
}
app.use(cors(corsOptions));

app.use(express.json()); //在其他路由中间件前（尽可能靠前，以能够通过bodyPaser获取req.body）
app.use(express.urlencoded({ extended: false }));

//托管静态资源
app.use(express.static("public"));

//注册路由,建议放在其他中间件的后面
app.use("/user",userRouter);
app.use("/category",categoryRouter);
app.use("/product",productRouter);
app.use("/product_image",product_imageRouter);
app.use("/cart",cartRouter);
app.use(homeRouter);
app.use("/order",orderRouter);
app.use("/address",addressRouter);

app.use('/upload', multerUpload);
app.use("/collection",collectionRouter);

const port = 5050;
app.listen(port, () => console.log(`绿植城后端服务（v2.0）已启动，端口:${port} ......`));
